package com.atzxh.datastructure.Stack;

import java.util.Scanner;

/**
 * @author 小红同学
 * @create 2022-10-09-16:10
 * @description
 */
public class Calculator {

    public static void main(String[] args) {

        String expression = "";
        System.out.println("请输入表达式:");
        Scanner scanner = new Scanner(System.in);
        expression = scanner.next();

        ArrayStackPlus numStack = new ArrayStackPlus(10);
        ArrayStackPlus opeStack = new ArrayStackPlus(10);

        //定义相关的变量
        int index = 0;  //用于扫描
        int num1 = 0;
        int num2 = 0;
        int ope = 0;
        int result = 0;
        char ch = ' ';

        while (true){
            ch = expression.substring(index,index + 1).charAt(0);

            //判断ch是数字还是运算符
            if (opeStack.isOpe(ch)){

                if (opeStack.isEmpty()){
                    opeStack.push(ch);
                }else {

                    if (opeStack.priority(ch) <= opeStack.priority((opeStack.getTop()))){
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        ope = opeStack.pop();
                        result = numStack.cal(num1,num2,ope);
                        numStack.push(result);
                        opeStack.push(ch);
                    }else {
                        opeStack.push(ch);
                    }
                }
            }else{
                numStack.push(ch-48);
            }

            index++;
            if (index >= expression.length()){
                break;
            }
        }


        while (true){
            if (opeStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            ope = opeStack.pop();
            result = numStack.cal(num1,num2,ope);
            numStack.push(result);

        }

        System.out.println("表达式：" + expression + "的值为：" + numStack.getTop());
    }
}

class ArrayStackPlus{
    private int maxSize;
    private int[] stack;
    private int top = -1;

    public ArrayStackPlus(int maxSize){
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    //栈满
    public boolean isFull(){
        return top == maxSize -1;
    }

    //栈空
    public boolean isEmpty(){
        return top == -1;
    }

    public int getTop(){
        return stack[top];
    }
    //入栈
    public void push(int value){
        if (isFull()){
            System.out.println("栈满!!!");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop(){
        if (isEmpty()){
            //抛异常
            throw new RuntimeException("栈空");
        }

        int value = stack[top];
        top--;
        return value;
    }

    //遍历
    public void list(){
        if (isEmpty()){
            System.out.println("栈空！！！");
        }

        for (int i = top; i >= 0; i--){
            System.out.printf("stack[%d] = %d\n",i,stack[i]);
        }
    }

    //返回操作符的优先级
    public int priority(int ope){
        if (ope == '+' || ope == '-'){
            return 0;
        }
        if (ope == '*' || ope == '/'){
            return 1;
        }
        return -1;
    }

    //判断运算符是否合法
    public boolean isOpe(char value){
        return value == '+' || value == '-' || value == '*' || value == '/';
    }

    //两个数运算
    public int cal(int num1, int num2, int ope){
        int result = 0;
        switch (ope){
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
            default:
                System.out.println("运算符不合法！！！");
                break;
        }
        return result;
    }
}